﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CP.Utility.Shreldder;
using System.Windows.Forms;

namespace CP.Utility
{
    public static class IEnumerableExtensions
    {
        public static DataTable ToDataTableE(this IEnumerable source)
        {
            Shredder ms = new Shredder(new DefaultShredderOptionsProvider(), new InjectionObjectShredder(), new DefaultSchemaBuilder());
            
            return ms.Shred(source);
        }

        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (var item in source)
                action(item);
        }

        public static IEnumerable<T> FindAllChildrenByType<T>(this Control control)
        {
            IEnumerable<Control> controls = control.Controls.Cast<Control>();
            return controls
                .OfType<T>()
                .Concat<T>(controls.SelectMany<Control, T>(ctrl => FindAllChildrenByType<T>(ctrl)));
        }
    }
}
